package com.agricultural;

import com.agricultural.environment.EnvironmentMonitor;
import com.agricultural.facade.AgriculturalSystemFacade;
import com.agricultural.observer.EnvironmentObserver;
import com.agricultural.proxy.SecureAgriculturalSystemFacade;
import com.agricultural.security.SecurityManager;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 使用安全代理
        AgriculturalSystemFacade systemFacade = new SecureAgriculturalSystemFacade();
        SecurityManager securityManager = SecurityManager.getInstance();
        EnvironmentMonitor environmentMonitor = new EnvironmentMonitor();

        // 登录
        boolean loggedIn = false;
        while (!loggedIn) {
            System.out.print("请输入用户名: ");
            String username = scanner.nextLine();
            System.out.print("请输入密码: ");
            String password = scanner.nextLine();
            loggedIn = securityManager.login(username, password);
        }

        // 初始化系统
        systemFacade.initializeSystem();
        System.out.println("系统已初始化，输入 'help' 获取命令列表");

        // 添加环境监测观察者
        ConsoleEnvironmentObserver observer = new ConsoleEnvironmentObserver();
        environmentMonitor.addObserver(observer);

        // 命令循环
        String command;
        boolean isMonitoring = false;

        while (true) {
            System.out.print("\n> ");
            command = scanner.nextLine().trim().toLowerCase();

            if (command.equals("exit")) {
                if (securityManager.hasPermission("ADMIN")) {
                    if (isMonitoring) {
                        environmentMonitor.stopMonitoring();
                    }
                    systemFacade.shutdownSystem();
                    scanner.close();
                    break;
                } else {
                    System.out.println("权限不足，只有管理员才能关闭系统");
                }
            }

            switch (command) {
                case "help":
                    printHelp();
                    break;
                case "start_monitoring":
                    if (securityManager.hasPermission("ADMIN")) {
                        if (!isMonitoring) {
                            environmentMonitor.startMonitoring();
                            isMonitoring = true;
                            System.out.println("环境监测已启动，每6秒更新一次数据");
                            System.out.println("输入 'stop_monitoring' 停止监测");
                        } else {
                            System.out.println("环境监测已在运行中");
                        }
                    } else {
                        System.out.println("权限不足，只有管理员能启动环境监测");
                    }
                    break;
                case "stop_monitoring":
                    if (isMonitoring) {
                        environmentMonitor.stopMonitoring();
                        isMonitoring = false;
                        System.out.println("环境监测已停止");
                    } else {
                        System.out.println("环境监测未运行");
                    }
                    break;
                case "create_equipment":
                    System.out.print("请输入设备类型: ");
                    String type = scanner.nextLine();
                    System.out.print("请输入设备编号: ");
                    String code = scanner.nextLine();
                    systemFacade.createEquipment(type, code);
                    break;
                case "start_equipment":
                    System.out.print("请输入设备编号: ");
                    systemFacade.startEquipment(scanner.nextLine());
                    break;
                case "perform_task":
                    System.out.print("请输入设备编号: ");
                    systemFacade.performTask(scanner.nextLine());
                    break;
                case "handle_emergency":
                    System.out.print("请输入设备编号: ");
                    String equipCode = scanner.nextLine();
                    System.out.print("请输入紧急情况类型 (obstacle/low_fuel): ");
                    String emergencyType = scanner.nextLine();
                    systemFacade.handleEmergency(equipCode, emergencyType);
                    break;
                case "refuel_equipment":
                    System.out.print("请输入设备编号: ");
                    systemFacade.refuelEquipment(scanner.nextLine());
                    break;
                case "switch_path_planner":
                    System.out.print("请输入切换状态 (true/false): ");
                    systemFacade.switchPathPlanner(Boolean.parseBoolean(scanner.nextLine()));
                    break;
                case "show_history":
                    systemFacade.showCommandHistory();
                    break;
                case "logout":
                    if (isMonitoring) {
                        environmentMonitor.stopMonitoring();
                        isMonitoring = false;
                    }
                    securityManager.logout();
                    loggedIn = false;
                    while (!loggedIn) {
                        System.out.print("请输入用户名: ");
                        String newUsername = scanner.nextLine();
                        System.out.print("请输入密码: ");
                        String newPassword = scanner.nextLine();
                        loggedIn = securityManager.login(newUsername, newPassword);
                    }
                    break;
                default:
                    System.out.println("未知命令，请输入 'help' 获取命令列表");
            }
        }

//        // 登出并关闭系统
//        securityManager.logout();
//        systemFacade.shutdownSystem();
//        scanner.close();
    }

    private static void printHelp() {
        System.out.println("\n可用命令:");
        System.out.println("help - 显示此帮助信息");
        System.out.println("start_monitoring - 开始环境数据自动更新");
        System.out.println("stop_monitoring - 停止环境数据更新");
        System.out.println("create_equipment - 创建新设备");
        System.out.println("start_equipment - 启动设备");
        System.out.println("perform_task - 执行任务");
        System.out.println("handle_emergency - 处理紧急情况");
        System.out.println("refuel_equipment - 为设备加油");
        System.out.println("switch_path_planner - 切换路径规划器");
        System.out.println("show_history - 显示命令历史");
        System.out.println("logout - 注销当前用户并登录新用户");
        System.out.println("exit - 退出系统");
    }

    // 环境观察者实现
    static class ConsoleEnvironmentObserver implements EnvironmentObserver {
        @Override
        public void update(EnvironmentMonitor monitor, String message) {
            System.out.println("[环境监测] " + message);
        }
    }
}